<template>
  <div>
    <a-alert message="开通短信" type="info" show-icon style="margin-bottom: 18px">
      <template #description>
        使用阿里云 短信服务，开通地址：<a
          href="https://www.aliyun.com/product/sms?userCode=oeeqid0s"
          target="_blank"
        >
          点我开通
        </a>
        <br />
        使用腾讯云 短信服务，开通地址：<a
          href="https://cloud.tencent.com/act/cps/redirect?redirect=10068&cps_key=b0589ee97f52eb6c5b0cf48561fd56a7"
          target="_blank"
        >
          点我开通
        </a>
        <br />
        使用短信宝 短信服务，开通地址：<a href="http://www.smsbao.com/reg?r=10452" target="_blank">
          点我开通
        </a>
        <br />
        短信API：
        <a href="https://doc.21ds.cn/detail?doc=4731341806141859/4823388385710965" target="_blank">
          对接文档
        </a>
      </template>
    </a-alert>
    <div class="table-page-search-wrapper">
      <a-row type="flex" justify="space-between" align="middle">
        <a-col :span="16">
          <a-form layout="inline" :model="listQuery">
            <a-form-item label="搜索项">
              <a-input v-model:value="listQuery.title" placeholder="输入标题/ID搜索..." />
            </a-form-item>
            <a-form-item>
              <a-button type="primary" @click="searchSubmit"> 搜索 </a-button>
            </a-form-item>
            <a-form-item>
              <a-button
                v-permission="{ action: 'del_sms_server_batch' }"
                type="primary"
                danger
                :disabled="selectedRowKeys.length === 0"
                @click="deleteSelect"
              >
                <template #icon>
                  <DeleteOutlined />
                </template>
                删除所选
              </a-button>
            </a-form-item>
          </a-form>
        </a-col>
        <a-col :span="8">
          <div style="margin-right: 0px; float: right; display: flex; align-item: center">
            <a-tooltip placement="top">
              <template #title>
                <span>刷新列表</span>
              </template>
              <a-button @click="reloadList">
                <template #icon><SyncOutlined /></template>
              </a-button>
            </a-tooltip>
            <a-button
              v-permission="{ action: 'sms_server_add' }"
              type="primary"
              style="margin-left: 12px"
              @click="addNewData"
            >
              <template #icon><PlusOutlined /></template> 新增短信配置
            </a-button>
          </div>
        </a-col>
      </a-row>
    </div>
    <a-table
      :columns="columns"
      :rowKey="(record) => record._id"
      :dataSource="dataList.data"
      :loading="loading"
      :pagination="pagination"
      :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      @change="handleTableChange"
    >
      <template #ak_from="{ text: record }">
        <a-tooltip v-if="record == 'platform'" placement="top">
          <template #title>
            <span>凭证由【系统管理】-【凭证管理】统一配置</span>
          </template>
          <a-tag color="blue">凭证管理</a-tag>
        </a-tooltip>
        <a-tag v-if="record == 'alone'" color="orange">单独设置</a-tag>
      </template>
      <template #status="{ text: record }">
        <a-tag v-if="record == 1" color="green">启用</a-tag>
        <a-tag v-if="record == 0" color="red">关闭</a-tag>
      </template>
      <template #action="{ text: record }">
        <a v-permission="{ action: 'sms_server_add' }" @click="sms_server_edit(record)"
          ><EditOutlined /> 编辑</a
        >
        <a-divider v-permission="{ action: 'sms_server_ext' }" type="vertical" />
        <a-dropdown>
          <a v-permission="{ action: 'sms_server_ext' }" class="ant-dropdown-link">
            更多 <DownOutlined
          /></a>
          <template #overlay>
            <a-menu>
              <a-menu-item v-permission="{ action: 'sms_server_copy' }">
                <a @click="sms_server_copy(record)"><CopyOutlined /> 复用</a>
              </a-menu-item>
              <a-menu-item v-permission="{ action: 'sms_server_del' }" style="color: red">
                <a @click="sms_server_delete(record)"><DeleteOutlined /> 删除</a>
              </a-menu-item>
            </a-menu>
          </template>
        </a-dropdown>
      </template>
    </a-table>
    <a-drawer
      width="50%"
      :title="drawerTitle"
      placement="right"
      :open="drawerShow"
      @close="
        () => {
          drawerShow = false
        }
      "
    >
      <template #extra>
        <a-button
          style="margin-right: 8px"
          @click="
            () => {
              drawerShow = false
            }
          "
          >取消</a-button
        >
        <a-button type="primary" @click="addServer">提交</a-button>
      </template>
      <a-form
        ref="formRef"
        :model="FormState"
        :rules="FormRule"
        :labelCol="labelCol"
        :wrapperCol="wrapperCol"
      >
        <a-tabs v-model:activeKey="activeKey" type="card">
          <a-tab-pane key="tab1" tab="短信设置">
            <a-form-item label="标题" has-feedback required name="title">
              <a-input v-model:value="FormState.title" placeholder="请输入标题" />
            </a-form-item>
            <a-form-item label="短信平台" has-feedback required name="platform">
              <a-radio-group v-model:value="FormState.platform">
                <a-radio key="aliyun" value="aliyun">阿里云</a-radio>
                <a-radio key="tencent" value="tencent">腾讯云</a-radio>
                <a-radio key="huawei" value="huawei">华为云</a-radio>
                <a-radio key="jdcloud" value="jdcloud">京东云</a-radio>
                <a-radio key="smsbao" value="smsbao">短信宝</a-radio>
              </a-radio-group>
            </a-form-item>
            <a-form-item label="配置状态" has-feedback required name="status">
              <a-radio-group v-model:value="FormState.status">
                <a-radio :key="1" :value="1">启用</a-radio>
                <a-radio :key="0" :value="0">关闭</a-radio>
              </a-radio-group>
            </a-form-item>
            <a-form-item
              v-if="FormState.status === 0"
              label="关闭说明"
              has-feedback
              required
              name="close_msg"
            >
              <a-input v-model:value="FormState.close_msg" placeholder="请输入配置关闭说明" />
            </a-form-item>
          </a-tab-pane>
          <a-tab-pane v-if="FormState.platform == 'aliyun'" key="tab2" tab="阿里云短信设置">
            <a-form-item label="凭证来源" has-feedback required name="ak_from">
              <a-radio-group v-model:value="FormState.ak_from">
                <a-radio key="platform" value="platform">在[凭证管理]中选择</a-radio>
                <a-radio key="alone" value="alone">单独设置</a-radio>
              </a-radio-group>
            </a-form-item>
            <a-form-item v-if="FormState.ak_from == 'platform'" label="选择凭证" name="ak_id">
              <a-select
                v-model:value="FormState.ak_id"
                show-search
                placeholder="请选择(支持搜索)"
                :default-active-first-option="false"
                :show-arrow="true"
                :filter-option="false"
                :options="akList"
                :fieldNames="{ value: '_id', label: 'title' }"
                @search="searchInput"
              >
                <template #notFoundContent>
                  <a-empty
                    image="https://img.alicdn.com/imgextra/i3/573804794/O1CN01IO097G1lHhdtoisyG_!!573804794.png"
                    :image-style="{
                      height: '90px'
                    }"
                  >
                    <template #description>
                      <view style="color: rgba(0, 0, 0, 0.88); padding-top: 20px">
                        暂无可选项，请先
                        <router-link :to="{ name: 'AkManage' }" target="_blank">添加</router-link>
                      </view>
                    </template>
                  </a-empty>
                </template>
              </a-select>
            </a-form-item>
            <a-form-item
              v-if="FormState.ak_from == 'alone'"
              label="AccessKeyId"
              name="aliyun_key_id"
            >
              <a-input v-model:value="FormState.aliyun_key_id" placeholder="请填写密钥ID">
                <template #suffix>
                  <a href="https://ram.console.aliyun.com/manage/ak" target="_blank">
                    <a-tooltip title="点击跳转官网获取">
                      <QuestionCircleOutlined />
                    </a-tooltip>
                  </a>
                </template>
              </a-input>
            </a-form-item>
            <a-form-item
              v-if="FormState.ak_from == 'alone'"
              label="AccessKeySecret"
              name="aliyun_key_secret"
            >
              <a-input
                v-model:value="FormState.aliyun_key_secret"
                placeholder="请填写访问密钥"
                :rows="2"
              >
                <template #suffix>
                  <a href="https://ram.console.aliyun.com/manage/ak" target="_blank">
                    <a-tooltip title="点击跳转官网获取">
                      <QuestionCircleOutlined />
                    </a-tooltip>
                  </a>
                </template>
              </a-input>
            </a-form-item>
            <a-form-item label="地域" name="aliyun_region">
              <a-select v-model:value="FormState.aliyun_region" placeholder="请选择地域">
                <a-select-option key="cn-hangzhou" value="cn-hangzhou"> 杭州 </a-select-option>
              </a-select>
            </a-form-item>
            <a-form-item label="短信签名" name="aliyun_sign">
              <a-input v-model:value="FormState.aliyun_sign" placeholder="请填写短信签名">
                <template #suffix>
                  <a href="https://dysms.console.aliyun.com/domestic/text" target="_blank">
                    <a-tooltip title="点击跳转官网获取">
                      <QuestionCircleOutlined />
                    </a-tooltip>
                  </a>
                </template>
              </a-input>
            </a-form-item>
            <a-form-item label="通知形式" has-feedback required name="platform_send_type">
              <a-radio-group v-model:value="FormState.platform_send_type">
                <a-radio key="text" value="text">文本短信</a-radio>
                <a-radio key="voice" value="voice">电话语音</a-radio>
              </a-radio-group>
            </a-form-item>
            <a-form-item
              v-if="FormState.platform_send_type == 'voice'"
              label="被叫显号"
              name="aliyun_called_number"
            >
              <a-input v-model:value="FormState.aliyun_called_number" placeholder="请填写被叫显号">
                <template #suffix>
                  <a href="https://dyvms.console.aliyun.com/service/list" target="_blank">
                    <a-tooltip
                      title="若您使用的语音通知文件为专属模式外呼，则必须传入已购买的号码，仅支持一个号码。点击跳转官网获取"
                    >
                      <QuestionCircleOutlined />
                    </a-tooltip>
                  </a>
                </template>
              </a-input>
            </a-form-item>
          </a-tab-pane>
          <a-tab-pane v-if="FormState.platform == 'tencent'" key="tab3" tab="腾讯云短信设置">
            <a-form-item label="凭证来源" has-feedback required name="ak_from">
              <a-radio-group v-model:value="FormState.ak_from">
                <a-radio key="platform" value="platform">在[凭证管理]中选择</a-radio>
                <a-radio key="alone" value="alone">单独设置</a-radio>
              </a-radio-group>
            </a-form-item>
            <a-form-item v-if="FormState.ak_from == 'platform'" label="选择凭证" name="ak_id">
              <a-select
                v-model:value="FormState.ak_id"
                show-search
                placeholder="请选择(支持搜索)"
                :default-active-first-option="false"
                :show-arrow="true"
                :filter-option="false"
                :options="akList"
                :fieldNames="{ value: '_id', label: 'title' }"
                @search="searchInput"
              >
                <template #notFoundContent>
                  <a-empty
                    image="https://img.alicdn.com/imgextra/i3/573804794/O1CN01IO097G1lHhdtoisyG_!!573804794.png"
                    :image-style="{
                      height: '90px'
                    }"
                  >
                    <template #description>
                      <view style="color: rgba(0, 0, 0, 0.88); padding-top: 20px">
                        暂无可选项，请先
                        <router-link :to="{ name: 'AkManage' }" target="_blank">添加</router-link>
                      </view>
                    </template>
                  </a-empty>
                </template>
              </a-select>
            </a-form-item>
            <a-form-item
              v-if="FormState.ak_from == 'alone'"
              label="腾讯云SecretId"
              name="tenc_secret_id"
            >
              <a-input v-model:value="FormState.tenc_secret_id" placeholder="请填写密钥ID">
                <template #suffix>
                  <a href="https://console.cloud.tencent.com/cam/capi" target="_blank">
                    <a-tooltip title="点击跳转官网获取">
                      <QuestionCircleOutlined />
                    </a-tooltip>
                  </a>
                </template>
              </a-input>
            </a-form-item>
            <a-form-item
              v-if="FormState.ak_from == 'alone'"
              label="腾讯云SecretKey"
              name="tenc_secret_key"
            >
              <a-input
                v-model:value="FormState.tenc_secret_key"
                placeholder="请填写访问密钥"
                :rows="2"
              >
                <template #suffix>
                  <a href="https://console.cloud.tencent.com/cam/capi" target="_blank">
                    <a-tooltip title="点击跳转官网获取">
                      <QuestionCircleOutlined />
                    </a-tooltip>
                  </a>
                </template>
              </a-input>
            </a-form-item>
            <a-form-item label="SDK AppID" name="tenc_sdk_appid">
              <a-input v-model:value="FormState.tenc_sdk_appid" placeholder="请填写短信SdkAppId">
                <template #suffix>
                  <a href="https://console.cloud.tencent.com/smsv2/app-manage" target="_blank">
                    <a-tooltip title="点击跳转官网获取">
                      <QuestionCircleOutlined />
                    </a-tooltip>
                  </a>
                </template>
              </a-input>
            </a-form-item>
            <a-form-item label="地域" name="tenc_region">
              <a-select v-model:value="FormState.tenc_region" placeholder="请选择地域">
                <a-select-option key="ap-beijing" value="ap-beijing">
                  华北地区(北京)
                </a-select-option>
                <a-select-option key="ap-guangzhou" value="ap-guangzhou">
                  华南地区(广州)
                </a-select-option>
                <a-select-option key="ap-nanjing" value="ap-nanjing">
                  华东地区(南京)
                </a-select-option>
              </a-select>
            </a-form-item>
            <a-form-item label="短信签名" name="tenc_sign">
              <a-input v-model:value="FormState.tenc_sign" placeholder="请填写短信签名">
                <template #suffix>
                  <a href="https://console.cloud.tencent.com/smsv2/csms-sign" target="_blank">
                    <a-tooltip title="点击跳转官网获取">
                      <QuestionCircleOutlined />
                    </a-tooltip>
                  </a>
                </template>
              </a-input>
            </a-form-item>
            <a-form-item label="通知形式" has-feedback required name="platform_send_type">
              <a-radio-group v-model:value="FormState.platform_send_type">
                <a-radio key="text" value="text">文本短信</a-radio>
                <a-radio key="voice" value="voice" disabled>电话语音</a-radio>
              </a-radio-group>
            </a-form-item>
            <!-- <a-form-item
              v-if="FormState.platform_send_type == 'voice'"
              label="被叫显号"
              name="platform_send_type"
            >
              <a-input v-model:value="FormState.tenc_called_number" placeholder="请填写被叫显号">
                <template #suffix>
                  <a href="https://console.cloud.tencent.com/smsv2/csms-sign" target="_blank">
                    <a-tooltip title="点击跳转官网获取">
                      <QuestionCircleOutlined />
                    </a-tooltip>
                  </a>
                </template>
              </a-input>
            </a-form-item> -->
          </a-tab-pane>
          <a-tab-pane v-if="FormState.platform == 'huawei'" key="tab4" tab="华为云短信设置">
            <a-form-item label="凭证来源" has-feedback required name="ak_from">
              <a-radio-group v-model:value="FormState.ak_from">
                <a-radio key="platform" value="platform">在[凭证管理]中选择</a-radio>
                <a-radio key="alone" value="alone">单独设置</a-radio>
              </a-radio-group>
            </a-form-item>
            <a-form-item v-if="FormState.ak_from == 'platform'" label="选择凭证" name="ak_id">
              <a-select
                v-model:value="FormState.ak_id"
                show-search
                placeholder="请选择(支持搜索)"
                :default-active-first-option="false"
                :show-arrow="true"
                :filter-option="false"
                :options="akList"
                :fieldNames="{ value: '_id', label: 'title' }"
                @search="searchInput"
              >
                <template #notFoundContent>
                  <a-empty
                    image="https://img.alicdn.com/imgextra/i3/573804794/O1CN01IO097G1lHhdtoisyG_!!573804794.png"
                    :image-style="{
                      height: '90px'
                    }"
                  >
                    <template #description>
                      <view style="color: rgba(0, 0, 0, 0.88); padding-top: 20px">
                        暂无可选项，请先
                        <router-link :to="{ name: 'AkManage' }" target="_blank">添加</router-link>
                      </view>
                    </template>
                  </a-empty>
                </template>
              </a-select>
            </a-form-item>
            <a-form-item
              v-if="FormState.ak_from == 'alone'"
              label="Application Key"
              name="huawei_app_key"
            >
              <a-input v-model:value="FormState.huawei_app_key" placeholder="请填写Application Key">
                <template #suffix>
                  <a
                    href="https://console.huaweicloud.com/msgsms/?region=cn-north-4#/msgsms/applicationManage"
                    target="_blank"
                  >
                    <a-tooltip title="点击跳转官网获取">
                      <QuestionCircleOutlined />
                    </a-tooltip>
                  </a>
                </template>
              </a-input>
            </a-form-item>
            <a-form-item
              v-if="FormState.ak_from == 'alone'"
              label="Application Secret"
              name="huawei_app_secret"
            >
              <a-input
                v-model:value="FormState.huawei_app_secret"
                placeholder="请填写Application Secret"
              >
                <template #suffix>
                  <a
                    href="https://console.huaweicloud.com/msgsms/?region=cn-north-4#/msgsms/applicationManage"
                    target="_blank"
                  >
                    <a-tooltip title="点击跳转官网获取">
                      <QuestionCircleOutlined />
                    </a-tooltip>
                  </a>
                </template>
              </a-input>
            </a-form-item>
            <a-form-item label="APP接入地址" name="huawei_app_url">
              <a-input
                v-model:value="FormState.huawei_app_url"
                placeholder="请填写APP接入地址"
                :rows="2"
              >
                <template #suffix>
                  <a
                    href="https://console.huaweicloud.com/msgsms/?region=cn-north-4#/msgsms/applicationManage"
                    target="_blank"
                  >
                    <a-tooltip title="点击跳转官网获取">
                      <QuestionCircleOutlined />
                    </a-tooltip>
                  </a>
                </template>
              </a-input>
            </a-form-item>
            <a-form-item label="短信签名通道号" name="huawei_sign_no">
              <a-input v-model:value="FormState.huawei_sign_no" placeholder="请填写短信签名通道号">
                <template #suffix>
                  <a
                    href="https://console.huaweicloud.com/msgsms/?region=cn-north-4#/msgsms/signatureManage"
                    target="_blank"
                  >
                    <a-tooltip title="点击跳转官网获取">
                      <QuestionCircleOutlined />
                    </a-tooltip>
                  </a>
                </template>
              </a-input>
            </a-form-item>
            <a-form-item label="短信签名" name="huawei_sign">
              <a-input v-model:value="FormState.huawei_sign" placeholder="请填写短信签名">
                <template #suffix>
                  <a
                    href="https://console.huaweicloud.com/msgsms/?region=cn-north-4#/msgsms/signatureManage"
                    target="_blank"
                  >
                    <a-tooltip title="点击跳转官网获取">
                      <QuestionCircleOutlined />
                    </a-tooltip>
                  </a>
                </template>
              </a-input>
            </a-form-item>
            <a-form-item label="通知形式" has-feedback required name="platform_send_type">
              <a-radio-group v-model:value="FormState.platform_send_type">
                <a-radio key="text" value="text">文本短信</a-radio>
                <a-radio key="voice" value="voice" disabled>电话语音</a-radio>
              </a-radio-group>
            </a-form-item>
          </a-tab-pane>
          <a-tab-pane v-if="FormState.platform == 'smsbao'" key="tab5" tab="短信宝短信设置">
            <a-form-item label="凭证来源" has-feedback required name="ak_from">
              <a-radio-group v-model:value="FormState.ak_from">
                <a-radio key="platform" value="platform">在[凭证管理]中选择</a-radio>
                <a-radio key="alone" value="alone">单独设置</a-radio>
              </a-radio-group>
            </a-form-item>
            <a-form-item v-if="FormState.ak_from == 'platform'" label="选择凭证" name="ak_id">
              <a-select
                v-model:value="FormState.ak_id"
                show-search
                placeholder="请选择(支持搜索)"
                :default-active-first-option="false"
                :show-arrow="true"
                :filter-option="false"
                :options="akList"
                :fieldNames="{ value: '_id', label: 'title' }"
                @search="searchInput"
              >
                <template #notFoundContent>
                  <a-empty
                    image="https://img.alicdn.com/imgextra/i3/573804794/O1CN01IO097G1lHhdtoisyG_!!573804794.png"
                    :image-style="{
                      height: '90px'
                    }"
                  >
                    <template #description>
                      <view style="color: rgba(0, 0, 0, 0.88); padding-top: 20px">
                        暂无可选项，请先
                        <router-link :to="{ name: 'AkManage' }" target="_blank">添加</router-link>
                      </view>
                    </template>
                  </a-empty>
                </template>
              </a-select>
            </a-form-item>
            <a-form-item v-if="FormState.ak_from == 'alone'" label="用户名" name="smsbao_username">
              <a-input
                v-model:value="FormState.smsbao_username"
                placeholder="请填写在短信宝平台注册的用户名"
              >
                <template #suffix>
                  <a href="http://console.smsbao.com/#/account/infomation" target="_blank">
                    <a-tooltip title="点击跳转官网获取">
                      <QuestionCircleOutlined />
                    </a-tooltip>
                  </a>
                </template>
              </a-input>
            </a-form-item>
            <a-form-item
              v-if="FormState.ak_from == 'alone'"
              label="登录密码/ApiKey"
              name="smsbao_password"
            >
              <a-input
                v-model:value="FormState.smsbao_password"
                placeholder="请填写平台登录密码MD5后的值(32位，不区分大小写)或者ApiKey(推荐使用更安全)可在短信宝后台或联系客服获得"
              >
                <template #suffix>
                  <a href="http://console.smsbao.com/#/isms/overview" target="_blank">
                    <a-tooltip title="点击跳转官网获取">
                      <QuestionCircleOutlined />
                    </a-tooltip>
                  </a>
                </template>
              </a-input>
            </a-form-item>
            <a-form-item label="短信签名" name="smsbao_sign">
              <a-input v-model:value="FormState.smsbao_sign" placeholder="请填写短信签名" />
            </a-form-item>
            <a-form-item label="通知形式" has-feedback required name="platform_send_type">
              <a-radio-group v-model:value="FormState.platform_send_type">
                <a-radio key="text" value="text">文本短信</a-radio>
                <a-radio key="voice" value="voice" disabled>电话语音</a-radio>
              </a-radio-group>
            </a-form-item>
          </a-tab-pane>
          <a-tab-pane v-if="FormState.platform == 'jdcloud'" key="tab7" tab="京东云短信设置">
            <a-form-item label="凭证来源" has-feedback required name="ak_from">
              <a-radio-group v-model:value="FormState.ak_from">
                <a-radio key="platform" value="platform">在[凭证管理]中选择</a-radio>
                <a-radio key="alone" value="alone">单独设置</a-radio>
              </a-radio-group>
            </a-form-item>
            <a-form-item v-if="FormState.ak_from == 'platform'" label="选择凭证" name="ak_id">
              <a-select
                v-model:value="FormState.ak_id"
                show-search
                placeholder="请选择(支持搜索)"
                :default-active-first-option="false"
                :show-arrow="true"
                :filter-option="false"
                :options="akList"
                :fieldNames="{ value: '_id', label: 'title' }"
                @search="searchInput"
              >
                <template #notFoundContent>
                  <a-empty
                    image="https://img.alicdn.com/imgextra/i3/573804794/O1CN01IO097G1lHhdtoisyG_!!573804794.png"
                    :image-style="{
                      height: '90px'
                    }"
                  >
                    <template #description>
                      <view style="color: rgba(0, 0, 0, 0.88); padding-top: 20px">
                        暂无可选项，请先
                        <router-link :to="{ name: 'AkManage' }" target="_blank">添加</router-link>
                      </view>
                    </template>
                  </a-empty>
                </template>
              </a-select>
            </a-form-item>
            <a-form-item
              v-if="FormState.ak_from == 'alone'"
              label="Access Key ID"
              name="jdcloud_access_key_id"
            >
              <a-input v-model:value="FormState.jdcloud_access_key_id" placeholder="请填写">
                <template #suffix>
                  <a href="https://uc.jdcloud.com/account/accesskey" target="_blank">
                    <a-tooltip title="点击跳转官网获取">
                      <QuestionCircleOutlined />
                    </a-tooltip>
                  </a>
                </template>
              </a-input>
            </a-form-item>
            <a-form-item
              v-if="FormState.ak_from == 'alone'"
              label="Access Key Secret"
              name="jdcloud_access_key_secret"
            >
              <a-input v-model:value="FormState.jdcloud_access_key_secret" placeholder="请填写">
                <template #suffix>
                  <a href="https://uc.jdcloud.com/account/accesskey" target="_blank">
                    <a-tooltip title="点击跳转官网获取">
                      <QuestionCircleOutlined />
                    </a-tooltip>
                  </a>
                </template>
              </a-input>
            </a-form-item>
            <a-form-item label="短信签名" name="jdcloud_sign">
              <a-input v-model:value="FormState.jdcloud_sign" placeholder="请填写短信签名" />
            </a-form-item>
            <a-form-item label="短信签名ID" name="jdcloud_sign_id">
              <a-input v-model:value="FormState.jdcloud_sign_id" placeholder="请填写短信签名ID" />
            </a-form-item>
            <a-form-item label="通知形式" has-feedback required name="platform_send_type">
              <a-radio-group v-model:value="FormState.platform_send_type">
                <a-radio key="text" value="text">文本短信</a-radio>
                <a-radio key="voice" value="voice" disabled>电话语音</a-radio>
              </a-radio-group>
            </a-form-item>
          </a-tab-pane>
          <a-tab-pane key="tab6" tab="对接密钥">
            <a-form-item label="SecretKey" name="server_secret">
              <a-input-search
                v-model:value="FormState.server_secret"
                placeholder="点击右侧“获取密钥”按钮进行获取"
                enter-button="获取密钥"
                @search="resetSecret"
              />
            </a-form-item>
          </a-tab-pane>
        </a-tabs>
      </a-form>
    </a-drawer>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, toRefs, onMounted, ref, watch } from 'vue'
import {
  Row,
  Col,
  Modal,
  Divider,
  Image,
  Drawer,
  Switch,
  InputPassword,
  InputNumber,
  Tabs,
  TypographyText,
  TabPane
} from 'ant-design-vue'
import {
  SyncOutlined,
  PlusOutlined,
  EditOutlined,
  DeleteOutlined,
  DownOutlined,
  CopyOutlined,
  QuestionCircleOutlined
} from '@ant-design/icons-vue'
import { delServerItem, delServerBatch, getServerList, editServerInfo } from '@/api/sms'
import { getAllAkList } from '@/api/ak'
import { randomString } from '@/utils/common'
import { useRouter } from 'vue-router'
import { requestFailed, requestSuccess } from '@/utils/feedback'

export default defineComponent({
  name: 'AdsList',
  components: {
    SyncOutlined,
    PlusOutlined,
    DownOutlined,
    CopyOutlined,
    EditOutlined,
    TypographyText,
    QuestionCircleOutlined,
    DeleteOutlined,
    [Tabs.name]: Tabs,
    [TabPane.name]: TabPane,
    [Modal.name]: Modal,
    [Row.name]: Row,
    [Drawer.name]: Drawer,
    [Switch.name]: Switch,
    [InputPassword.name]: InputPassword,
    [InputNumber.name]: InputNumber,
    [Col.name]: Col,
    [Image.name]: Image,
    [Divider.name]: Divider
  },
  setup() {
    const formRef = ref()
    const router = useRouter()
    const state = reactive<any>({
      loading: true,
      drawerShow: false,
      modalVisible: false,
      testEmail: '',
      searchText: '',
      drawerTitle: '短信配置详情',
      activeKey: 'tab1',
      testServer: {
        _id: ''
      },
      dataList: [],
      akList: [],
      selectedRowKeys: [],
      selectedKeysPage: {},
      listQuery: {
        pageNo: 1,
        pageSize: 15,
        title: ''
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 20 }
      },
      pagination: {
        pageSizeOptions: ['15', '30', '50'],
        showSizeChanger: true,
        total: 1,
        showTotal: (total) => `共 ${total} 条数据`,
        pageSize: 15,
        current: 1
      },
      columns: [
        {
          title: 'ID',
          dataIndex: '_id'
        },
        {
          title: '标题',
          dataIndex: 'title'
        },
        {
          title: '短信平台',
          dataIndex: 'platform_title'
        },
        {
          title: '凭证来源',
          dataIndex: 'ak_from',
          slots: { customRender: 'ak_from' }
        },
        {
          title: '配置状态',
          dataIndex: 'status',
          slots: { customRender: 'status' }
        },
        {
          title: '添加时间',
          dataIndex: 'create_time'
        },
        {
          title: '操作',
          width: '150px',
          slots: { customRender: 'action' }
        }
      ]
    })
    onMounted(() => {
      getDataList()
    })

    function handleTableChange(e) {
      state.loading = true
      state.pagination.pageSize = e.pageSize
      state.listQuery.pageSize = e.pageSize
      state.listQuery.pageNo = e.current
      state.pagination.current = e.current
      getDataList()
    }
    function resetSecret() {
      FormState.server_secret = randomString(64)
    }

    const FormState = reactive<any>({
      _id: '',
      title: '',
      platform: '',
      status: 1
    })
    const FormRule = {
      title: [{ required: true, message: '请输入标题', trigger: ['blur', 'change'] }],
      status: [{ required: true, message: '请输选择配置状态', trigger: ['blur', 'change'] }],
      platform: [{ required: true, message: '请选择短信平台', trigger: ['blur', 'change'] }]
    }

    watch(
      () => FormState.platform,
      () => {
        getAkList()
        state.searchText = ''
      }
    )
    function getAkList() {
      getAllAkList({ platform: FormState.platform, title: state.searchText }).then((res) => {
        state.akList = res
      })
    }
    function searchInput(v) {
      state.searchText = v
      getAkList()
    }

    function reloadList() {
      state.loading = true
      state.listQuery.pageNo = 1
      state.pagination.current = 1
      getDataList()
    }

    function searchSubmit() {
      state.loading = true
      state.listQuery.pageNo = 1
      state.pagination.current = 1
      getDataList()
    }
    function sms_server_copy(itemData) {
      for (let item in FormState) {
        FormState[item] = ''
      }
      state.drawerTitle = '复制短信配置'
      for (let item in itemData) {
        if (item != '_id' && item != 'server_secret') {
          let copyTips = item == 'title' ? ' - 复制' : ''
          FormState[item] = itemData[item] + copyTips
        }
      }
      state.drawerShow = true
    }

    function getDataList() {
      getServerList(state.listQuery).then((res) => {
        state.loading = false
        state.dataList = res
        state.pagination.total = res.totalCount
      })
    }
    function sms_server_edit(res) {
      state.drawerTitle = '编辑短信配置'
      for (let item in FormState) {
        FormState[item] = ''
      }
      for (let item in res) {
        FormState[item] = res[item]
      }
      state.drawerShow = true
    }
    function addNewData() {
      for (let item in FormState) {
        FormState[item] = ''
      }
      state.drawerTitle = '新增短信配置'
      state.drawerShow = true
    }
    function sms_server_delete(data) {
      Modal.confirm({
        title: '确认永久删除本配置?',
        content: '【' + data._id + '】',
        okType: 'danger',
        okText: '确认删除',
        onOk() {
          delServerItem({ id: data._id }).then((res) => {
            if (res.code === 200) {
              state.loading = true
              getDataList()
              requestSuccess(res)
            } else {
              requestFailed(res)
            }
          })
        }
      })
    }
    const addServer = () => {
      formRef.value
        .validate()
        .then(() => {
          editServerInfo(FormState).then((res) => {
            if (res.code === 200) {
              requestSuccess(res)
              formRef.value.resetFields()
              state.loading = true
              state.drawerShow = false
              reloadList()
            } else {
              requestFailed(res)
            }
          })
        })
        .catch(() => {
          return false
        })
    }
    function deleteSelect() {
      Modal.confirm({
        title: '确认批量删除所选内容?',
        okType: 'danger',
        okText: '确认删除',
        onOk() {
          delServerBatch({ ids: state.selectedRowKeys })
            .then((res) => {
              if (res.code === 200) {
                state.loading = true
                getDataList()
                requestSuccess(res)
              } else {
                requestFailed(res)
              }
            })
            .finally(() => {
              state.selectedRowKeys = []
            })
        }
      })
    }
    const onSelectChange = (selectedRowKeys) => {
      state.selectedKeysPage[state.listQuery.pageNo] = selectedRowKeys
      let nowList: any[] = []
      for (const key in state.selectedKeysPage) {
        nowList = nowList.concat(state.selectedKeysPage[key])
      }
      const uniqueList = [...new Set(nowList)]
      state.selectedRowKeys = uniqueList
    }
    return {
      ...toRefs(state),
      handleTableChange,
      deleteSelect,
      onSelectChange,
      reloadList,
      resetSecret,
      searchSubmit,
      sms_server_delete,
      formRef,
      searchInput,
      addServer,
      FormState,
      FormRule,
      addNewData,
      router,
      sms_server_edit,
      sms_server_copy,
      getDataList
    }
  }
})
</script>

<style scoped></style>
